﻿/*
 * ──────────────────────────────────────────────────────────────
 *　描   述：
 *　作   者：王浩                                              
 *　版   本：1.0
 *　创建时间：2022/9/8 10:43:25                             
 * ──────────────────────────────────────────────────────────────
 *　命名空间: FD_DB                                   
 *　类   名: FileDB                                         
 * ──────────────────────────────────────────────────────────────
 */
using FD_DB.DB.File;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace FD_DB
{
    /// <summary>
    /// 配置文件
    /// </summary>
    public class FileDB
    {
        /// <summary>
        /// 数据
        /// </summary>
        public FileModel? Model { get; set; }
        string _fileName = "diledb";
        public FileDB()
        {
            IniDB();
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void IniDB()
        {
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + _fileName))
            {
                var text = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + _fileName);
                if (text != "")
                {
                    Model = JsonSerializer.Deserialize<FileModel>(text);
                }

            }
            if (Model == null)
            {
                Model = new FileModel();
                Model.Port = 3434;
                Model.Edition = 12;
                Model.EditionTime = DateTime.Now;
                Model.SourcePath = "D:\\Study\\文档";
                Model.FtpPass = "wh";
                Model.FtpUser = "wh";
                Updata();
            }
        }
        public void Updata()
        {
            if (Model != null)
            {

                if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + _fileName))
                {
                    using (FileStream fs = new(AppDomain.CurrentDomain.BaseDirectory + _fileName, FileMode.Create))
                    {
                         fs.Close();
                    }
                }
                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + _fileName, JsonSerializer.Serialize(Model), Encoding.UTF8);
            }
        }

    }
}
